package algorithms2.sort;

import java.util.Arrays;

/**
 * @Author: DRevelation
 * @Date: 2021/9/13 10:44
 */
public class ShellSort {

    public static void main(String[] args) {
        int[] nums = {5, 3, 2, 1, 4};
        int[] sortedNums = shellSort(nums);
        System.out.println(Arrays.toString(sortedNums));
    }

    private static int[] shellSort(int[] nums) {
        int len = nums.length;
        int k = 0;
        for (int gap = len / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < len; i++) {
                for (int j = i - gap; j >= 0; j -= gap) {
                    if (nums[j] > nums[j + gap]) {
                        Utils.swap(nums, j, j + gap);
                    }
                }
                k++;
                System.out.println("第 " + k + " 次排序结果是: " + Arrays.toString(nums));
            }
        }
        return nums;
    }
}
